package com.kuhyygy.activity;

import com.kuhyygy.R;
import com.kuhyygy.service.BinderService;
import com.kuhyygy.service.MsgQueueService;
import com.kuhyygy.service.SimpleIntentService;
import com.kuhyygy.service.BinderService.MyBinder;
import com.kuhyygy.status.NotificationService;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 
 * @author chenjunwen
 *
 */
public class ServiceActivity extends Activity {
	
	private static final String TAG = ServiceActivity.class.getName();
	
	private Button startBtn;
	private Button stopBtn;

	private boolean flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.services);
		startBtn = (Button) findViewById(R.id.startKServ);
		stopBtn = (Button) findViewById(R.id.stopKServ);
		startBtn.setOnClickListener(clickLst);
		stopBtn.setOnClickListener(clickLst);

		Button bindStart = (Button) findViewById(R.id.startBinderServ);
		Button bindStop = (Button) findViewById(R.id.stopBinderServ);
		bindStart.setOnClickListener(clickLst);
		bindStop.setOnClickListener(clickLst);
		
		Button loopBtn = (Button) findViewById(R.id.addMsg);
		loopBtn.setOnClickListener(clickLst);
		
		Button downBtn = (Button) findViewById(R.id.notifyBtn);
		downBtn.setOnClickListener(clickLst);
	}
	

	@Override
	protected void onStart() {
		super.onStart();
		NotificationManager nfm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nfm.cancel(R.id.notifyBtn);
	}


	private OnClickListener clickLst = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent it = new Intent(ServiceActivity.this, SimpleIntentService.class);
			Intent it2 = new Intent(ServiceActivity.this, MsgQueueService.class);
			switch (v.getId()) {
			case R.id.startKServ:
				Log.i(TAG, "主线程开启, ID:" + Thread.currentThread().getId());
				startService(it);
				break;
			case R.id.stopKServ:
				stopService(it);
				break;
			case R.id.startBinderServ:
				startBindService();
				break;
			case R.id.stopBinderServ:
				stopBindService();
				break;
			case R.id.addMsg: {
				startService(it2);
				break;
			}
			case R.id.notifyBtn: {
				Intent i = new Intent(ServiceActivity.this, NotificationService.class);
				startService(i);
				break;
			}
			default:
				break;
			}
		}

	};

	private void startBindService() {
		Intent i = new Intent(ServiceActivity.this, BinderService.class);
		bindService(i, conn, Context.BIND_AUTO_CREATE);
	}

	private void stopBindService() {
		if (flag) {
			unbindService(conn);
		}
	}

	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			flag = false;
		}
		

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			MyBinder myBinder = (MyBinder) service;
			BinderService bindService = myBinder.getService();
			bindService.doMethod();
			flag = true;
		}
	};
}
